﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Events.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Engine.*;

    public class GiftSellDialog extends SlotDialog {

        private const NUM_SLOTS:int = 6;

        private var m_usedTypes:Object = null;
        protected var m_buttons:Array;
        private var m_sellAll:GenericButton;
        private var m_sendGifts:GenericButton;
        private var m_totalGiftPrices:Number;
        private var m_itemObject:Object;

        public function GiftSellDialog(){
            this.m_buttons = new Array();
            m_dialogAsset = "assets/dialogs/FV_GiftsSell.swf";
            super();
        }
        override protected function getNumSlots():int{
            return (this.NUM_SLOTS);
        }
        override protected function onLoadComplete():void{
            m_window = (m_loader.content as MovieClip);
            this.m_itemObject = new Object();
            m_window.gifts_mc.close_bt.addEventListener(MouseEvent.CLICK, this.onCloseClick);
            m_window.gifts_mc.arrowLt_bt.addEventListener(MouseEvent.CLICK, onLtArrowClick);
            m_window.gifts_mc.arrowRt_bt.addEventListener(MouseEvent.CLICK, onRtArrowClick);
            this.m_sellAll = new GenericButton(m_window.gifts_mc.sellAll_bt, this.onSellAllClick);
            this.m_sellAll.text = ZLoc.t("Dialogs", "SellAll");
            this.m_sendGifts = new GenericButton(m_window.gifts_mc.sendGift_bt, this.onSendGiftsClick);
            this.m_sendGifts.text = ZLoc.t("Dialogs", "SendGifts");
            m_tiles.push(m_window.gifts_mc.giftPanel1, m_window.gifts_mc.giftPanel2, m_window.gifts_mc.giftPanel3, m_window.gifts_mc.giftPanel4, m_window.gifts_mc.giftPanel5, m_window.gifts_mc.giftPanel6);
            removeTiles();
            this.pushNewData();
            var _local1:int;
            while ((((_local1 < this.NUM_SLOTS)) && ((_local1 < m_data.length)))) {
                this.addComponent(_local1);
                _local1++;
            };
            addChild(m_window);
        }
        private function pushNewData():void{
            var _local2:int;
            var _local3:Object;
            var _local4:Object;
            var _local5:Item;
            m_data = [];
            m_tileSet = 0;
            this.m_totalGiftPrices = 0;
            this.m_usedTypes = new Object();
            var _local1:Array = Global.player.getGifts();
            if (_local1){
                _local2 = 0;
                while (_local2 < _local1.length) {
                    _local3 = new Object();
                    _local4 = _local1[_local2];
                    if (_local4.item){
                        _local5 = _local4.item;
                        this.m_totalGiftPrices = (this.m_totalGiftPrices + (_local5.sellPrice * _local4.num));
                        if (this.m_usedTypes[_local4.item.name] == null){
                            this.m_usedTypes[_local4.item.name] = _local4.num;
                            _local3.name = _local5.name;
                            _local3.itemName = _local5.name;
                            _local3.className = _local5.className;
                            _local3.rank = 0;
                            _local3.type = _local5.type;
                            _local3.localizedName = _local5.localizedName;
                            _local3.cost = _local5.cost;
                            if (((_local4.senderArray) && (_local4.senderArray[0]))){
                                _local3.giftSenderId = _local4.senderArray[0];
                            } else {
                                _local3.giftSenderId = 0;
                            };
                            _local3.compId = _local2;
                            if (_local5.coinYield){
                                _local3.coinYield = _local5.coinYield;
                            };
                            if (_local5.growTime){
                                _local3.harvestTime = _local5.growTime;
                            };
                            if (_local5.sizeX){
                                _local3.sizeX = _local5.sizeX;
                            };
                            if (_local5.sizeY){
                                _local3.sizeX = _local5.sizeY;
                            };
                            m_data.push(_local3);
                        };
                    };
                    _local2++;
                };
            };
            m_data.sort(this.getRankComparison);
            this.fixArrows();
        }
        private function getRankComparison(_arg1:Object, _arg2:Object):int{
            var _local3:int;
            if (((((_arg1.hasOwnProperty("rank")) && (_arg2.hasOwnProperty("rank")))) && (!((_arg1.rank == _arg2.rank))))){
                if (_arg2.rank > _arg1.rank){
                    _local3 = 1;
                } else {
                    _local3 = -1;
                };
            };
            return (_local3);
        }
        override protected function fixArrows():void{
            m_window.gifts_mc.arrowLt_bt.visible = ((m_tileSet)>0) ? true : false;
            m_window.gifts_mc.arrowRt_bt.visible = ((((m_tileSet + 1) * this.getNumSlots()))<m_data.length) ? true : false;
        }
        override protected function addComponent(_arg1:int, _arg2:int=0):void{
            var mc:* = null;
            var sellBackRatio:* = NaN;
            var url:* = null;
            var icon:* = null;
            var tempName:* = null;
            var senderName:* = null;
            var compId:* = _arg1;
            var index:int = _arg2;
            if (((m_tiles) && (!((m_data[(index + compId)] == null))))){
                mc = (m_tiles[compId] as MovieClip);
                m_tiles[compId].visible = true;
                m_tiles[compId].itemTitle_tf.text = m_data[(index + compId)].localizedName;
                if (((m_data[(index + compId)].giftSenderId) && (!((m_data[(index + compId)].giftSenderId == -1))))){
                    tempName = Global.player.getFriendName(m_data[(index + compId)].giftSenderId);
                    if (tempName){
                        senderName = tempName.split(" ");
                        if (senderName[0]){
                            m_tiles[compId].friendName_tf.text = senderName[0];
                        } else {
                            m_tiles[compId].friendName_tf.text = tempName;
                        };
                        if (this.m_usedTypes[m_data[(index + compId)].name] > 1){
                            m_tiles[compId].friendName_tf.text = (m_tiles[compId].friendName_tf.text + (" " + ZLoc.t("Dialogs", "SellAnd")));
                        };
                    } else {
                        m_tiles[compId].friendName_tf.text = ZLoc.t("Dialogs", "SellFV");
                    };
                } else {
                    m_tiles[compId].friendName_tf.text = ZLoc.t("Dialogs", "SellFV");
                };
                sellBackRatio = Global.gameSettings.getNumber("sellBackRatio");
                m_tiles[compId].hoverText_mc.yield_tf.visible = true;
                m_tiles[compId].hoverText_mc.yield_tf.text = ZLoc.t("Dialogs", "Yield", {coins:Math.floor((m_data[(index + compId)].cost * sellBackRatio))});
                if (m_data[(index + compId)].harvestTime.toString() != ""){
                    m_tiles[compId].hoverText_mc.growth_tf.visible = true;
                    m_tiles[compId].hoverText_mc.growth_tf.text = ZLoc.t("Dialogs", "GrowthDays", {time:m_data[(index + compId)].harvestTime});
                } else {
                    m_tiles[compId].hoverText_mc.growth_tf.visible = false;
                };
                m_tiles[compId].giftAmount_tf.text = ZLoc.t("Dialogs", "GiftAmount", {amount:this.m_usedTypes[m_data[(index + compId)].name]});
                m_tiles[compId].hoverText_mc.visible = false;
                m_tiles[compId].icon.addEventListener(MouseEvent.MOUSE_OUT, this.onOut);
                m_tiles[compId].icon.addEventListener(MouseEvent.MOUSE_OVER, this.onOver);
                this.m_buttons[compId] = new Array();
                this.m_buttons[compId][0] = new GenericButton(m_tiles[compId].use_bt, this.onUseGift);
                this.m_buttons[compId][0].text = ZLoc.t("Dialogs", "Use");
                this.m_buttons[compId][1] = new GenericButton(m_tiles[compId].sell_bt, this.onSellGift);
                this.m_buttons[compId][1].text = ZLoc.t("Dialogs", "Sell");
                url = Global.gameSettings.getImageByName(m_data[(index + compId)].name, "icon");
                icon = LoadingManager.load(url, function (_arg1:Event):void{
                    var _local2:*;
                    if (((icon) && (icon.content))){
                        if (m_tiles[compId]){
                            Utilities.removeAllChildren(m_tiles[compId].icon);
                        };
                        _local2 = icon.content;
                        _local2.width = 50;
                        _local2.height = 50;
                        m_tiles[compId].icon.addChild(_local2);
                    };
                });
            };
        }
        override protected function postRemoveTiles(_arg1:int):void{
            if (m_tiles[_arg1].use_bt){
                m_tiles[_arg1].use_bt.removeEventListener(MouseEvent.CLICK, this.onUseGift);
            };
            if (m_tiles[_arg1].sell_bt){
                m_tiles[_arg1].sell_bt.removeEventListener(MouseEvent.CLICK, this.onSellGift);
            };
        }
        private function onCloseClick(_arg1:MouseEvent):void{
            removeTiles(false);
            this.m_usedTypes = null;
            m_window.gifts_mc.close_bt.removeEventListener(MouseEvent.CLICK, this.onCloseClick);
            m_window.gifts_mc.arrowLt_bt.removeEventListener(MouseEvent.CLICK, onLtArrowClick);
            m_window.gifts_mc.arrowRt_bt.removeEventListener(MouseEvent.CLICK, onRtArrowClick);
            _arg1.stopPropagation();
            close();
            dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
        }
        private function onSellAllClick(_arg1:MouseEvent):void{
            this.disableButtons();
            if (Global.player.getGiftCount() > 0){
                UI.displayMessage(ZLoc.t("Dialogs", "SellAllGifts", {total:this.m_totalGiftPrices}), GenericPopup.TYPE_YESNO, this.sellAllGiftsHandler, "sellAllGiftsHandler", true);
            } else {
                StatsManager.count("farmville_sell_gift_screen", "click_sell_all_item", "gifts_empty_impression");
                UI.displayMessage(ZLoc.t("Dialogs", "NoMoreGiftsToSell"), GenericPopup.TYPE_YESNO, this.noMoreGiftsHandler, "noMoreGiftsHandler", true);
            };
        }
        private function onSendGiftsClick(_arg1:MouseEvent):void{
            this.disableButtons();
            GlobalEngine.socialNetwork.redirect("gifts.php?ref=send_gift_box");
        }
        private function onUseGift(_arg1:MouseEvent):void{
            var _local2:String = _arg1.target.parent.name;
            var _local3:int = (parseInt(_local2.charAt((_local2.length - 1))) - 1);
            var _local4:int = (_local3 + (m_tileSet * this.NUM_SLOTS));
            var _local5:Object = m_data[_local4];
            var _local6:String = _local5.type;
            var _local7:String = _local5.name;
            var _local8:String = _local5.className;
            StatsManager.count("farmville_sell_gift_screen", "click_use_item", _local7);
            switch (_local6){
                case "contract":
                    dispatchEvent(new MarketEvent(MarketEvent.MARKET_GIFT, MarketEvent.CONTRACT, _local7));
                    break;
                case "residence":
                    dispatchEvent(new MarketEvent(MarketEvent.MARKET_GIFT, MarketEvent.RESIDENCE, _local7));
                    break;
                case "business":
                    dispatchEvent(new MarketEvent(MarketEvent.MARKET_GIFT, MarketEvent.BUSINESS, _local7));
                    break;
                case "factory":
                    dispatchEvent(new MarketEvent(MarketEvent.MARKET_GIFT, MarketEvent.GENERIC, _local7));
                    break;
                case "decoration":
                    dispatchEvent(new MarketEvent(MarketEvent.MARKET_GIFT, MarketEvent.GENERIC, _local7));
                    break;
            };
            this.onCloseClick(_arg1);
        }
        private function onSellGift(_arg1:MouseEvent):void{
            var _local2:String = _arg1.target.parent.name;
            var _local3:int = (parseInt(_local2.charAt((_local2.length - 1))) - 1);
            var _local4:int = (_local3 + (m_tileSet * this.NUM_SLOTS));
            this.disableButtons();
            this.m_itemObject = m_data[_local4];
            var _local5:Item = Global.gameSettings.getItemByName(this.m_itemObject.itemName);
            UI.displayMessage(ZLoc.t("Main", "SellObjectSpecific", {
                item:_local5.localizedName,
                coins:_local5.sellPrice
            }), GenericPopup.TYPE_YESNO, this.sellOneGiftHandler, "sellOneGiftHandler", true);
        }
        protected function sellAllGiftsHandler(_arg1:GenericPopupEvent):void{
            var _local2 = (_arg1.button == GenericPopup.YES);
            if (_local2 == true){
                StatsManager.count("farmville_sell_gift_screen", "click_sell_all_item", "all_item");
                GameTransactionManager.addTransaction(new TSellStoredItem(null, true, this.onSellComplete), true, true);
                Global.player.removeAllGifts();
                this.onCloseClick(new MouseEvent(MouseEvent.CLICK));
            } else {
                this.disableButtons(false);
            };
        }
        protected function noMoreGiftsHandler(_arg1:GenericPopupEvent):void{
            var _local2 = (_arg1.button == GenericPopup.YES);
            if (_local2 == true){
                StatsManager.count("farmville_sell_gift_screen", "click_sell_all_item", "gifts_empty_accept");
                GlobalEngine.socialNetwork.redirect("gifts.php?ref=empty_gift_box");
            } else {
                StatsManager.count("farmville_sell_gift_screen", "click_sell_all_item", "gifts_empty_cancel");
                this.disableButtons(false);
            };
        }
        protected function sellOneGiftHandler(_arg1:GenericPopupEvent):void{
            var _local3:MapResource;
            var _local2 = (_arg1.button == GenericPopup.YES);
            if (_local2 == true){
                StatsManager.count("farmville_sell_gift_screen", "click_sell_item", this.m_itemObject.itemName);
                GameTransactionManager.addTransaction(new TSellStoredItem(this.m_itemObject, false, this.onSellComplete), true, true);
                _local3 = new MapResource(this.m_itemObject.itemName);
                Global.player.removeGift(_local3);
                if (Global.player.getGiftCount() == 0){
                    this.onCloseClick(new MouseEvent(MouseEvent.CLICK));
                };
            } else {
                this.disableButtons(false);
            };
        }
        private function onSellComplete(_arg1:Object):void{
            var _local2:String;
            var _local3:int;
            this.disableButtons(false);
            if (_arg1){
                for (_local2 in _arg1.gifts) {
                    Global.player.gifts[_local2] = _arg1.gifts[_local2];
                };
                removeTiles();
                this.pushNewData();
                _local3 = 0;
                while ((((_local3 < this.NUM_SLOTS)) && ((_local3 < m_data.length)))) {
                    this.addComponent(_local3);
                    _local3++;
                };
                if (Global.player.getGiftCount() == 0){
                    StatsManager.count("farmville_sell_gift_screen", "click_sell_all_item", "gifts_empty_impression");
                };
            };
        }
        private function disableButtons(_arg1:Boolean=true):void{
            this.m_sellAll.disabled = _arg1;
            this.m_sendGifts.disabled = _arg1;
            if (_arg1){
                m_window.gifts_mc.close_bt.removeEventListener(MouseEvent.CLICK, this.onCloseClick);
            } else {
                m_window.gifts_mc.close_bt.addEventListener(MouseEvent.CLICK, this.onCloseClick);
            };
            if (((m_window.gifts_mc.arrowLt_bt) && (m_window.gifts_mc.arrowLt_bt))){
                m_window.gifts_mc.arrowLt_bt.enabled = !(_arg1);
                m_window.gifts_mc.arrowLt_bt.mouseEnabled = !(_arg1);
            };
            if (((m_window.gifts_mc.arrowRt_bt) && (m_window.gifts_mc.arrowRt_bt))){
                m_window.gifts_mc.arrowRt_bt.enabled = !(_arg1);
                m_window.gifts_mc.arrowRt_bt.mouseEnabled = !(_arg1);
            };
            var _local2:int;
            while ((((_local2 < this.NUM_SLOTS)) && ((_local2 < m_data.length)))) {
                this.m_buttons[_local2][0].toggleDisable(!(_arg1));
                this.m_buttons[_local2][1].toggleDisable(!(_arg1));
                _local2++;
            };
        }
        private function onOut(_arg1:MouseEvent):void{
            var _local2:String = _arg1.target.parent.name;
            var _local3:int = (parseInt(_local2.charAt((_local2.length - 1))) - 1);
            var _local4:int = (_local3 + (m_tileSet * this.NUM_SLOTS));
            m_tiles[(_local4 % this.NUM_SLOTS)].hoverText_mc.visible = false;
        }
        private function onOver(_arg1:MouseEvent):void{
            var _local2:String = _arg1.target.parent.name;
            var _local3:int = (parseInt(_local2.charAt((_local2.length - 1))) - 1);
            var _local4:int = (_local3 + (m_tileSet * this.NUM_SLOTS));
            m_tiles[(_local4 % this.NUM_SLOTS)].hoverText_mc.visible = true;
        }

    }
}//package Display 
